﻿// -------------------------------------------------------------------------
//    @FileName         :    NFServerLevelMgr.cpp
//    @Author           :    gaoyi
//    @Date             :    2024/12/19
//    @Email            :    445267987@qq.com
//    @Module           :    NFServerLevelMgr
//
// -------------------------------------------------------------------------

#include "NFServerLevelMgr.h"

#include <ServerLevelDescEx.h>

#include <NFComm/NFCore/NFTimeUtility.h>
#include <NFComm/NFPluginModule/NFIConfigModule.h>

NFServerLevelMgr::NFServerLevelMgr()
{
    if (EN_OBJ_MODE_INIT == NFShmMgr::Instance()->GetCreateMode())
    {
        CreateInit();
    }
    else
    {
        ResumeInit();
    }
}

NFServerLevelMgr::~NFServerLevelMgr()
{
}

int NFServerLevelMgr::CreateInit()
{
    m_bIsInit = false;
    m_iDayTimerId = INVALID_ID;
    m_iServerLevel = 0;
    m_tOpenTime = 0;
    return 0;
}
    
int NFServerLevelMgr::ResumeInit()
{
    return 0;
}

int NFServerLevelMgr::Init()
{
    m_bIsInit = true;
    auto pConfig = FindModule<NFIConfigModule>()->GetAppConfig(NF_ST_LOGIC_SERVER);
    CHECK_NULL(0, pConfig);
    uint64_t serverOpenTime = pConfig->GetServerOpenTime();
    m_iDayTimerId = SetDayCalender(0, 23, 59, 59);
    return SetOpenTime(serverOpenTime);
}

int NFServerLevelMgr::UpdateServerLevel()
{
    if (!m_bIsInit) return 0;

    int iOpenDays = GetOpenDays();

    m_iServerLevel = ServerLevelDescEx::Instance()->GetServerLevelByDays(iOpenDays);

    LOGSVR_DEBUG("update server level: " << m_iServerLevel << " open days: " << iOpenDays);

    return 0;
}

int NFServerLevelMgr::GetOpenDays()
{
    int iOpenDays = NFTimeUtility::GetDaysDelta(NF_ADJUST_TIMENOW(), m_tOpenTime);

    return iOpenDays + 1;
}

int NFServerLevelMgr::GetPlayerExpRate(int iLevel, double& fRate)
{
    if(m_bIsInit)
    {
        int iDelta = iLevel - m_iServerLevel;
        if(iDelta >= 1 && iDelta <= 2)
        {
            fRate = 0.5f;
        }
        else if(iDelta >= 3 && iDelta <= 4)
        {
            fRate = 0.4f;
        }
        else if(iDelta > 4)
        {
            fRate = 0.3f;
        }
        else if(iLevel >= 60)
        {
            if(iDelta <= -8)
            {
                fRate = 1.6f;
            }
            else if(iDelta >= -7 && iDelta <= -5)
            {
                fRate = 1.4f;
            }
            else if(iDelta >= -4 && iDelta <= -2)
            {
                fRate = 1.2f;
            }
            else
            {
                fRate = 1.0f;
            }
        }
        else
        {
            fRate = 1.0f;
        }

        LOGSVR_DEBUG("get rate success: " << fRate <<
            " server lv: " << m_iServerLevel <<
            " player lv: " << iLevel);
        return 0;
    }

    return -1;
}

int NFServerLevelMgr::SetOpenTime(time_t tTime)
{
    m_tOpenTime = tTime;
    UpdateServerLevel();
    return 0;
}

int NFServerLevelMgr::OnTimer(int timeId, int callcount)
{
    if (m_bIsInit)
    {
        UpdateServerLevel();
    }
    return 0;
}


